using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Services
{
    /// <summary>
    /// 发布管理服务实现
    /// 提供内容发布管理的业务逻辑实现，包括发布、撤销发布、历史查询和队列管理功能
    /// </summary>
    public class PublishingService : IPublishingAppService
    {
        /// <summary>
        /// 发布仓储接口
        /// </summary>
        private readonly Domain.Interfaces.IPublishRepository _publishRepository;

        /// <summary>
        /// 初始化发布管理服务实例
        /// </summary>
        /// <param name="publishRepository">发布仓储接口实例</param>
        public PublishingService(Domain.Interfaces.IPublishRepository publishRepository)
        {
            _publishRepository = publishRepository;
        }

        /// <summary>
        /// 发布内容到指定环境
        /// 将内容发布到生产环境或指定的目标环境
        /// </summary>
        /// <param name="contentId">内容标识符</param>
        /// <param name="publishTime">发布时间，可选</param>
        /// <param name="targetEnvironment">目标环境，可选</param>
        /// <returns>发布操作的异步任务</returns>
        public async Task PublishAsync(long contentId, string? publishTime = null, string? targetEnvironment = null)
        {
            await _publishRepository.PublishAsync(contentId, publishTime, targetEnvironment);
        }

        /// <summary>
        /// 撤销内容发布
        /// 将已发布的内容从生产环境或指定环境撤回
        /// </summary>
        /// <param name="contentId">内容标识符</param>
        /// <returns>撤销发布操作的异步任务</returns>
        public async Task UnpublishAsync(long contentId)
        {
            await _publishRepository.UnpublishAsync(contentId);
        }

        /// <summary>
        /// 获取发布历史记录
        /// 查询指定内容的发布历史记录，支持分页查询
        /// </summary>
        /// <param name="contentId">内容标识符</param>
        /// <param name="page">页码，从1开始</param>
        /// <param name="limit">每页记录数，默认10条</param>
        /// <returns>发布历史记录DTO列表</returns>
        public async Task<IEnumerable<Application.DTOs.PublishHistoryDto>> GetPublishHistory(long contentId, int page = 1, int limit = 10)
        {
            var history = await _publishRepository.GetPublishHistoryAsync(contentId, page, limit);
            return history.Select(p => new Application.DTOs.PublishHistoryDto
            {
                Id = p.Id,
                ContentId = p.ContentId,
                Status = p.Status,
                Version = p.Version,
                PublishedAt = p.PublishedAt,
                CreatedAt = p.CreatedAt
            });
        }

        /// <summary>
        /// 获取发布队列信息
        /// 查询当前的发布队列状态，支持按状态筛选和分页查询
        /// </summary>
        /// <param name="status">队列状态筛选条件，可选</param>
        /// <param name="page">页码，从1开始</param>
        /// <param name="limit">每页记录数，默认10条</param>
        /// <returns>发布队列DTO列表</returns>
        public async Task<IEnumerable<Application.DTOs.PublishQueueDto>> GetPublishQueueAsync(string? status = null, int page = 1, int limit = 10)
        {
            var queue = await _publishRepository.GetPublishQueueAsync(status, page, limit);
            return queue.Select(p => new Application.DTOs.PublishQueueDto
            {
                Id = p.Id,
                ContentId = p.ContentId,
                Status = p.Status,
                Version = p.Version,
                CreatedAt = p.CreatedAt
            });
        }

        /// <summary>
        /// 获取发布历史记录（重载方法）
        /// 查询指定内容的发布历史记录，支持分页查询
        /// </summary>
        /// <param name="contentId">内容标识符</param>
        /// <param name="page">页码</param>
        /// <param name="limit">每页记录数</param>
        /// <returns>发布历史记录DTO列表</returns>
        public async Task<IEnumerable<Application.DTOs.PublishHistoryDto>> GetPublishHistoryAsync(long contentId, int page, int limit)
        {
            var history = await _publishRepository.GetPublishHistoryAsync(contentId, page, limit);
            return history.Select(p => new Application.DTOs.PublishHistoryDto
            {
                Id = p.Id,
                ContentId = p.ContentId,
                Status = p.Status,
                Version = p.Version,
                PublishedAt = p.PublishedAt,
                CreatedAt = p.CreatedAt
            });
        }
    }
}
